﻿import { types, getEnv, flow } from 'mobx-state-tree';
import { ServerStat } from './ServerStat';

export const ServerStatsStore = types
  .model('ServerStatsStore', {
    stats: types.array(ServerStat),
    error: types.optional(types.string, ''),
  })
  .actions(self => ({
    load: flow(function* load() {
      const backendSrv = getEnv(self).backendSrv;
      const res = yield backendSrv.get('/api/admin/stats');
      self.stats.clear();
      self.stats.push(ServerStat.create({ name: '仪表板合计', value: res.dashboards }));
      self.stats.push(ServerStat.create({ name: '用户合计', value: res.users }));
      self.stats.push(ServerStat.create({ name: '活跃用户（过去 30 天）', value: res.activeUsers }));
      self.stats.push(ServerStat.create({ name: '组织合计', value: res.orgs }));
      self.stats.push(ServerStat.create({ name: '播放列表合计', value: res.playlists }));
//      self.stats.push(ServerStat.create({ name: '快照合计', value: res.snapshots }));
      self.stats.push(ServerStat.create({ name: '仪表板标签合计', value: res.tags }));
      self.stats.push(ServerStat.create({ name: '已标星号的仪表板合计', value: res.stars }));
      self.stats.push(ServerStat.create({ name: '告警合计', value: res.alerts }));
    }),
  }));
